package com.yugao.fintech.antelope.pay.payment.channel.wxpay;

import com.alibaba.fastjson2.JSONObject;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.yugao.fintech.antelope.pay.common.constants.Cs;
import com.yugao.fintech.antelope.pay.constant.IfCodeEnum;
import com.yugao.fintech.antelope.pay.model.entity.PayOrder;
import com.yugao.fintech.antelope.pay.payment.channel.AbstractPayOrderQueryService;
import com.yugao.fintech.antelope.pay.payment.channel.wxpay.utils.WxpayV3Util;
import com.yugao.fintech.antelope.pay.payment.model.bo.MchAppConfigContext;
import com.yugao.fintech.antelope.pay.payment.model.bo.WxServiceWrapper;
import com.yugao.fintech.antelope.pay.payment.model.params.wxpay.WxpayIsvsubMchParams;
import com.yugao.fintech.antelope.pay.payment.model.rqrs.ChannelRetMsgExt;
import com.yugao.fintech.antelope.pay.payment.service.ConfigContextQueryService;
import org.springframework.stereotype.Service;

/**
 * 微信查单接口
 *

 */
@Service
public class WxpayPayOrderQueryService extends AbstractPayOrderQueryService {

    @Autowired
    private ConfigContextQueryService configContextQueryService;

    @Override
    public IfCodeEnum getIfCode() {
        return IfCodeEnum.WXPAY;
    }

    @Override
    public ChannelRetMsgExt query(PayOrder payOrder, MchAppConfigContext mchAppConfigContext) {

        try {

            WxServiceWrapper wxServiceWrapper = (WxServiceWrapper) configContextQueryService
                    .getClientWrapper(this.getIfCode(), mchAppConfigContext);

            if (Cs.PAY_IF_VERSION.WX_V3.equals(wxServiceWrapper.getApiVersion())) {   //V3
                String reqUrl;
                String query;
                if (mchAppConfigContext.isIsvsubMch()) { // 特约商户
                    WxpayIsvsubMchParams isvsubMchParams = (WxpayIsvsubMchParams) configContextQueryService.queryIsvsubMchParams(mchAppConfigContext.getMchNo(), mchAppConfigContext.getAppId(), getIfCode());
                    reqUrl = String.format("/v3/pay/partner/transactions/out-trade-no/%s", payOrder.getPayOrderNo());
                    query = String.format("?sp_mchid=%s&sub_mchid=%s", wxServiceWrapper.getWxPayService().getConfig().getMchId(), isvsubMchParams.getSubMchId());
                } else {
                    reqUrl = String.format("/v3/pay/transactions/out-trade-no/%s", payOrder.getPayOrderNo());
                    query = String.format("?mchid=%s", wxServiceWrapper.getWxPayService().getConfig().getMchId());
                }
                JSONObject resultJSON = WxpayV3Util.queryOrderV3(reqUrl + query, wxServiceWrapper.getWxPayService());

                String channelState = resultJSON.getString("trade_state");
                if ("SUCCESS".equals(channelState)) {
                    return ChannelRetMsgExt.confirmSuccess(resultJSON.getString("transaction_id"));
                } else if ("USERPAYING".equals(channelState)
                        || "NOTPAY".equals(channelState)) { //支付中，等待用户输入密码
                    return ChannelRetMsgExt.waiting(); //支付中
                } else if ("CLOSED".equals(channelState)
                        || "REVOKED".equals(channelState)
                        || "PAYERROR".equals(channelState)) {  //CLOSED—已关闭， REVOKED—已撤销(刷卡支付), PAYERROR--支付失败(其他原因，如银行返回失败)
                    return ChannelRetMsgExt.confirmFail(); //支付失败
                } else {
                    return ChannelRetMsgExt.unknown();
                }

            } else {
                return ChannelRetMsgExt.unknown();
            }

        } catch (WxPayException e) {
            return ChannelRetMsgExt.sysError(e.getReturnMsg());
        } catch (Exception e) {
            return ChannelRetMsgExt.sysError(e.getMessage());
        }
    }

}
